package net.tngou.iask.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.math.NumberUtils;

import net.tngou.iask.dao.QuestionDao;
import net.tngou.iask.dao.SurveyDao;
import net.tngou.iask.entity.Td;
import net.tngou.iask.jdbc.PageUtil;
import net.tngou.iask.jdbc.QueryHelper;
import net.tngou.iask.pojo.Access;
import net.tngou.iask.pojo.Accessresult;
import net.tngou.iask.pojo.Answer;
import net.tngou.iask.pojo.Disease;
import net.tngou.iask.pojo.Health;
import net.tngou.iask.pojo.POJO;
import net.tngou.iask.pojo.Survey;
import net.tngou.iask.util.StringUtil;

/**
 * 问卷
 * @author tngou@tngou.net
 *
 */
public class SurveyService extends BaseService {
	/**
	 * 取得问卷
	 * @param page 当前页
	 * @param size 页大小
	 * @param main 实体（）
	 * @param type 类型
	 * @param keyword 搜索关键词
	 * @return
	 */
	public PageUtil getPage(int page,int size,long main,long type,String keyword) {
		SurveyDao dao = new SurveyDao();
		return dao.getPage(page, size, main, type, keyword);			
	}
	/**
	 * 更新问卷中间库
	 * @param survey 文件ID
	 * @param disease 疾病
	 * @param question 问题
	 */
	@Deprecated
	public void update(long survey,String[] disease,String[] question) {
		Long diseases[]=null;
		Long questions[]=null;
		if(disease!=null)diseases=StringUtil.StringToLong(disease);
		if(question!=null)questions=StringUtil.StringToLong(question);
		SurveyDao dao = new SurveyDao();
		dao.update(survey, diseases, questions);
	}
	
	
	/**
	 * 取得表格
	 * @param survey
	 * @param access
	 * @param full
	 * @return
	 */
	public List<List<Td>> getTrs(Survey survey,Access access,boolean full) {
		QuestionService questionService = new QuestionService();
		List<Map<String, Object>> list = questionService.getQuestionMap(survey.getId());
		
		if(access!=null&&full)
		{				
			for (int i = 0; i < list.size(); i++) {
				Map<String, Object> e = list.get(i);
				long question = Long.parseLong(e.get("question").toString());
				POJO pojo = new Accessresult().get(" access="+access.getId()+" AND patient="+access.getPatient()+" AND question="+question);
				if(pojo==null)//
				{
					list.remove(i);
					i--;
				}
			}		
		}
		
		List<List<Td>> trs=new ArrayList<>();	
		Map<String, Integer> tagmap= new HashMap<>();
			
		for (int i = 0; i < list.size(); ) {
			List<Td> tds = new ArrayList<>();

			Map<String, Object> q = list.get(0);
			String tag=q.get("tag").toString();
			if(StringUtil.isNotEmpty(tag))
			{
				int rowspan=0;
				if(tagmap.get(tag)==null)
				{
					for (int j = 0; j < list.size(); j++) {
						Map<String, Object> e = list.get(j);
						if(tag.equals(e.get("tag")))
						{
							if(j+1<list.size())
							{
								if(!e.get("seq").toString().equals(list.get(j+1).get("seq").toString()))
								{
									rowspan++;
								}
							}else
							{
								rowspan++;
							}
						}
					}
						
					tagmap.put(tag, rowspan);
					Td td0 = new Td(rowspan, 1, 1, 0, tag, "",0, 0, null, null);
					tds.add(td0);
				}
				
			}
			String name=q.get("name").toString();
			Td td1 = new Td(1, 2, 1, 1, "", name,0, 0, null, null);
			tds.add(td1);
			List<?> r1=null;
			List<?> v1=null;
			QuestionService service = new QuestionService();
			r1 = service.getAnswer(Long.parseLong(q.get("question").toString()));
			if(access!=null)
			{
				long question=Long.parseLong(q.get("question").toString());
				if(access.getType()==2)
                 v1=service.getList("access="+access.getId()+" AND patient="+access.getPatient()+" AND question="+question, Accessresult.class);
				if(v1==null)
				 v1=service.getList("patient="+access.getPatient()+" AND question="+question, Health.class);
			}
			if(list.size()>1)
			{
	
				Map<String, Object> q_next = list.get(1);
				if(q.get("seq").toString().equals(q_next.get("seq").toString()))
				{
				
					Td td2 = new Td(1, 4, 0, 2, "", "",Long.parseLong(q.get("question").toString()), Integer.parseInt(q.get("type").toString()), r1, v1);
					tds.add(td2);
					list.remove(0);
					name=q_next.get("name").toString();
					Td td3 = new Td(1, 2, 1, 1, "", name, 0,0, null,null);
					tds.add(td3);
					List<?> r2=null;
					List<?> v2=null;		
						r2 = service.getAnswer(Long.parseLong(q_next.get("question").toString()));
					if(access!=null)
					{
						long question=Long.parseLong(q_next.get("question").toString());
						if(access.getType()==2)
						v2=service.getList("access="+access.getId()+" AND patient="+access.getPatient()+" AND question="+question, Accessresult.class);				
						if(v2==null)
						v2=service.getList("patient="+access.getPatient()+" AND question="+question, Health.class);
						
					}
					Td td4 = new Td(1, 4, 0, 2, "", "",Long.parseLong(q_next.get("question").toString()), Integer.parseInt(q_next.get("type").toString()), r2,v2);
					tds.add(td4);
					list.remove(0);
				}else
				{
					Td td2 = new Td(1, 10, 0, 2, "", "",Long.parseLong(q.get("question").toString()), Integer.parseInt(q.get("type").toString()), r1, v1);
					tds.add(td2);
					list.remove(0);
				}
				
			}else
			{
				Td td2 = new Td(1, 10, 0, 2, "", "",Long.parseLong(q.get("question").toString()), Integer.parseInt(q.get("type").toString()), r1, v1);
				tds.add(td2);
				list.remove(0);
			}
						
			trs.add(tds);
			
		}
		
		return trs;
	}
	/**
	 * 取得疾病对于的案例
	 * @param disease
	 * @return
	 */
	public List<Survey> getSurvey(long disease) {
		SurveyDao dao = new SurveyDao();
		return dao.getSurvey(disease);	
	}
	/**
	 * 取得
	 * @param main
	 * @param type
	 * @return
	 */
	public List<Survey> getList(long main,long type) {
		SurveyDao dao = new SurveyDao();
		return dao.getList(main, type);
	}
	

}
